<script setup>
import { userGetmenu, userSetmenu, menuList } from "../../../api/index";
import { ref, reactive, onMounted, nextTick } from "vue";
import { Plus } from "@element-plus/icons-vue";

// 树形菜单权限数据
const permissionData = ref([]);

onMounted(() => {
  // 请求菜单权限信息
  userGetmenu().then(({ data }) => {
    // console.log(data);
    permissionData.value = data.data;
  });

  // 请求列表数据
  getListData();
});

// 列表数据，但是我不知道是什么列表，普通用户的列表？？
const paginationData = reactive({
  pageNum: 1,
  pageSize: 1,
});

// 每页条数
const handleSizeChange = (val) => {
  paginationData.pageSize = val;
  getListData();
};

// 页数
const handleCurrentChange = (val) => {
  paginationData.pageNum = val;
  getListData();
};

// 列表数据
const tableData = reactive({
  list: [],
  total: 0,
});

// 请求列表数据(复用)
const getListData = () => {
  menuList(paginationData).then(({ data }) => {
    const { list, total } = data.data;
    tableData.list = list;
    tableData.total = total;
  });
};

// 控制弹窗的显示和隐藏
const dialogFormVisable = ref(false);

// 点击关闭弹窗触发回调
const beforeClose = () => {
  dialogFormVisable.value = false;
  // 重置表单
  formRef.value.resetFields();
  // tree重置
  treeRef.value.setCheckedKeys(defaultKeys);
};

// 表单数据
const form = reactive({
  id: "",
  name: "",
  permissions: "",
});

// 默认选中的权限数据
const defaultKeys = [4, 5];

// 树形组件组件实例
const treeRef = ref();
// 表单实例
const formRef = ref();

// 点击表单确认提交
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 这里返回的不是数组吗，数组可以直接传？是
      // console.log(treeRef.value.getCheckedKeys());
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys());
      userSetmenu({
        name: form.name,
        permissions: permissions,
        id: form.id,
      }).then(({ data }) => {
        // 重置表单
        beforeClose();
        // 刷新列表数据
        getListData();
        // console.log(data);
        // console.log(99);
      });
    } else {
      console.log("error", fields);
    }
  });
};

// 打开编辑
const open = (rowData = {}) => {
  dialogFormVisable.value = true;
  // 回显选中权限setCurrentKey()
  // 弹窗打开form是异步的
  nextTick(() => {
    if (rowData) {
      // console.log(rowData);

      Object.assign(form, { id: rowData.id, name: rowData.name });
      treeRef.value.setCheckedKeys(rowData.permission);
    }
  });
};

// 表单校验
const rules = reactive({
  name: [{ required: true, trigger: "blur", message: "请输入权限名称" }],
});
</script>

<template>
  <panel-head></panel-head>
  <div class="btns">
    <el-button :icon="Plus" @click="open(null)" type="primary" size="small"
      >添加</el-button
    >
  </div>

  <!-- 表格 -->
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="permissionName" label="菜单权限" width="500px" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :background="false"
      size="small"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <el-dialog
    v-model="dialogFormVisable"
    :before-close="beforeClose"
    title="添加权限"
    width="500"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="form"
      :rules="rules"
    >
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称" />
      </el-form-item>

      <el-form-item label="权限" prop="permissions">
        <!-- 请求回来的数据,权限管理设置了 disabled:true,意思就是禁止选择 -->
        <el-tree
          ref="treeRef"
          style="max-width: 600px"
          :data="permissionData"
          show-checkbox
          node-key="id"
          :default-expanded-keys="[2]"
          :default-checked-keys="defaultKeys"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">
.btns {
  padding: 10px 0 10px 10px;
  background-color: #ffffff;
}
</style>
